/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：SearchAllServlet   
 * 类描述：   搜索所有接口
 * 创建人：zhanghl   
 * 创建时间：2016年12月2日 下午6:38:56   
 * 修改人：zhanghl   
 * 修改时间：2016年12月2日 下午6:38:56   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.SearchAllReqPara;
import com.lst.entity.response.SearchAllResPara;
import com.lst.model.MstGymnasium;
import com.lst.model.ProjectInfo;
import com.lst.service.MstGymnasiumService;
import com.lst.service.ProjectInfoService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: SearchAllServlet
 * @Description: 搜索所有接口
 * @author zhanghl
 * @date 2016年12月2日 下午6:38:56
 *
 */
public class SearchAllServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private ProjectInfoService projectInfoService = ctx.getBean(ProjectInfoService.class);

	private MstGymnasiumService mstGymnasiumService = ctx.getBean(MstGymnasiumService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		SearchAllResPara resPara = new SearchAllResPara();

		try {
			SearchAllReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer searchtype = Integer.valueOf(reqPara.getSeachtype());

				Map<String, Object> map = new HashMap<String, Object>();

				map.put("enabled", true);
				map.put("isDeleted", false);

				PageBounds pageBounds = new PageBounds();

				if(CommCode.SYSTEM_CARESEL_RANGE_PRO.equals(searchtype)){

					List<ProjectInfo> pilist = projectInfoService.queryByList(map, pageBounds);

					List<ProjectInfo> piList = null;

					if(!pilist.isEmpty()){						
						piList = new ArrayList<ProjectInfo>();
						for(ProjectInfo pi:pilist){
							if(pi.getLonggitude()!=null&&!("").equals(pi.getLonggitude())){
								if(pi.getLatitude()!=null&&!("").equals(pi.getLatitude())){
									piList.add(pi);
								}
							}
						}
					}
					resPara.setPilist(piList);
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);
				}else{

					List<MstGymnasium> mglist = mstGymnasiumService.queryByList(map, pageBounds);

					List<MstGymnasium> mgList = null;

					if(!mglist.isEmpty()){						
						mgList = new ArrayList<MstGymnasium>();
						for(MstGymnasium mg:mglist){
							if(mg.getLongitude()!=null&&!("").equals(mg.getLongitude())){
								if(mg.getLatitude()!=null&&!("").equals(mg.getLatitude())){
									mgList.add(mg);
								}
							}
						}

					}
					resPara.setMglist(mgList);	
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);
				}
			}
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info("SearchAllServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public SearchAllReqPara getReqPara(HttpServletRequest request) {

		SearchAllReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new SearchAllReqPara());

			reqPara = (SearchAllReqPara) baseRequest.clone();

			String searchtype = request.getParameter("searchtype");

			logger.info("reqPara searchwork: " + searchtype);

			reqPara.setSeachtype(StringUtils.isEmpty(searchtype) ? CommCode.SYSTEM_CARESEL_RANGE_PRO.toString() : searchtype );

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		/*boolean flag = false;

		SearchAllReqPara saReqPara = (SearchAllReqPara) reqPara;
		SearchAllResPara saResPara = (SearchAllResPara) resPara;

		if(StringUtils.isEmpty(saReqPara.getSeachtype())){
			saResPara.setCode(CommCode.M_A000000);
			saResPara.setMessage(CommCode.M_F000001);
		} else {
			flag = true;
		}

		return  flag;
		 */
		return true;
	}
}
